﻿Imports System.Data.OleDb
Public Class frmPhieuKho
    Public LoaiPhieu As String


    Private Sub frmPhieuKho_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            'load background
            Me.BackColor = Color.FromArgb(194, 217, 247)
            PhieuKho_Load()
        Catch ex As Exception

        End Try
    End Sub
#Region "Load Data"
    Protected Sub PhieuKho_Load()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dtPhieuKho As New DataTable
            Dim query As String = "Select LoaiPhieuID,TenPhieu,GhiChu,SoPhieuTuTang From LoaiPhieu Where Xoa=0 And LoaiPhieu='" & LoaiPhieu & "'"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtPhieuKho)
            dbConn.Close()
            dtPhieuKho.Columns.Add("STT")
            For i As Integer = 0 To dtPhieuKho.Rows.Count - 1
                dtPhieuKho.Rows(i)("STT") = i + 1
            Next
            dgvPhieuKho.AutoGenerateColumns = False
            dgvPhieuKho.DataSource = dtPhieuKho
        Catch ex As Exception

        End Try
    End Sub
#End Region
    Private Sub cmdAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddNew.Click
        Try
            frmThemPhieuKho.LoaiPhieu = LoaiPhieu
            ShowModalForm(frmThemPhieuKho, False, False)
            frmThemPhieuKho.LoaiPhieu = ""
            PhieuKho_Load()
        Catch ex As Exception

        End Try
    End Sub
    Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
        Try
            Dim LoaiPhieuID As Integer = Integer.Parse(dgvPhieuKho.Rows(dgvPhieuKho.CurrentCell.RowIndex).Cells("LoaiPhieuID").Value.ToString)
            frmThemPhieuKho.LoaiPhieuID = LoaiPhieuID
            ShowModalForm(frmThemPhieuKho, False, False)
            frmThemPhieuKho.LoaiPhieu = ""
            frmThemPhieuKho.LoaiPhieuID = -1
            PhieuKho_Load()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
        Try
            Dim LoaiPhieuID As Integer = Integer.Parse(dgvPhieuKho.Rows(dgvPhieuKho.CurrentCell.RowIndex).Cells("LoaiPhieuID").Value.ToString)
            Dim TenPhieu As String = dgvPhieuKho.Rows(dgvPhieuKho.CurrentCell.RowIndex).Cells("TenPhieu").Value.ToString
            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim dbCommand As OleDbCommand
            Dim query As String
            Dim dlgRes As DialogResult
            dlgRes = MessageBox.Show( _
                  "Bạn muốn xóa loại phiếu kho : " & TenPhieu, _
                  "Xóa loại phiếu kho", _
            MessageBoxButtons.OKCancel, _
                  MessageBoxIcon.Question)
            If dlgRes = DialogResult.OK Then
                query = "Update LoaiPhieu Set Xoa=1,NgayXoa=N'" & NgayTao() & "',NguoiXoa='" & NguoiTao() & "'  Where LoaiPhieuID=" & LoaiPhieuID
                dbCommand = New OleDbCommand(query, dbConn)
                dbCommand.ExecuteNonQuery()
                dbConn.Close()
                PhieuKho_Load()
            End If
        Catch ex As Exception

        End Try
    End Sub
End Class